package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    /**
     * 连接mysql数据库的操作，用户名，密码，使用jdbc连接
     */
    public static String username = "root";
    public static String password = "2609079796aA@";
    public static String url = "jdbc:mysql://localhost:3306/bms?characterEncoding=utf-8";

    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //连接数据库
    public static Connection getConnectDb(){
        Connection ct = null;
        try{
            ct = DriverManager.getConnection(url,username,password);
        } catch (SQLException e){
            e.printStackTrace();
        }
        return ct;
    }

    //关闭数据库
    public static void CloseDB(ResultSet rs, PreparedStatement stm, Connection conn){
        if(rs!=null)
        {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stm!=null)
        {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
//    static String url = "jdbc:mysql://localhost:3306/bms?useUnicode=true&characterEncoding=UTF-8";
//
//    static String user = "root";
//    static String pwd = "2609079796aA@";
//    static Connection ct = null;
//
//    public static Connection getConnectDb() {
//        //1.加载驱动
//        //Class.forName(driver);不必要，已经可以省略
//        //2.利用DriverManager
//        try {
//            ct = DriverManager.getConnection(url, user, pwd);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//        return ct;
//    }
}

